"""add_message_bill_association_table

Revision ID: 3344469e1610
Revises: 692fdfbd1567
Create Date: 2025-07-19 18:49:26.826059

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '3344469e1610'
down_revision: Union[str, None] = '692fdfbd1567'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('message_bills',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('message_id', sa.Integer(), nullable=False),
    sa.Column('bill_id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('confidence', sa.Float(), nullable=True),
    sa.ForeignKeyConstraint(['bill_id'], ['bills.id'], ),
    sa.ForeignKeyConstraint(['message_id'], ['chat_messages.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_message_bills_id'), 'message_bills', ['id'], unique=False)

    # Use batch mode for SQLite to drop column and constraint
    with op.batch_alter_table('chat_messages', schema=None) as batch_op:
        batch_op.drop_column('bill_id')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    # Use batch mode for SQLite to add column and constraint
    with op.batch_alter_table('chat_messages', schema=None) as batch_op:
        batch_op.add_column(sa.Column('bill_id', sa.INTEGER(), nullable=True))
        batch_op.create_foreign_key(None, 'bills', ['bill_id'], ['id'])

    op.drop_index(op.f('ix_message_bills_id'), table_name='message_bills')
    op.drop_table('message_bills')
    # ### end Alembic commands ###
